// index.js

Page({
	data: {
		list: [],
		fromdata: [{'Name':'','Phone':'','Address': '','FromMiniApp':''}],
		bannerimg: [],
		gbimg: [],
		serviceimg: [],
		enterpriseimg: [],
		phone: ''
  },
  Name(e) {
		let nameData = "fromdata.Name";
		this.setData({
			[nameData]: e.detail.value
		})
	},
	Phone(e) {
		let phoneData = "fromdata.Phone";
		this.setData({
			[phoneData]: e.detail.value
		})
	},
	Submit() {
		var that = this;
		let Name = this.data.fromdata.Name;
		let Phone = this.data.fromdata.Phone;
		let show = '';
		let ret = '';
		var rex = /^[0-9]+$/;
		var Num = rex.test(Phone);

		console.log(this.data.fromdata)

		for (let it in this.data.fromdata) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(this.data.fromdata[it]) + '&'
    }
    ret = ret.trim('&');
		console.log(ret)
		
		if (Name == undefined) {
      wx: wx.showToast({
        title: '请输入姓名',
        icon: 'error'
      })
      return false
    }else if (Phone == undefined) {
      wx: wx.showToast({
        title: '请输入手机号码',
        icon: 'error'
      })
      return false
    }else if (!Num) {
      wx: wx.showToast({
        title: '请输入纯数字',
        icon: 'error'
      })
      return false
    }else if (Phone.length < 11) {
      wx: wx.showToast({
        title: '请输入正确号码',
        icon: 'error'
      })
      return false
		}

		wx.showLoading({
			title: '提交中',
			mask: true
		})

		// 表单提交
		wx.request({
			url: 'https://form.api.weleyuan.com/v1/client/FormInfo/AddFormInfo',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: ret,
			method: 'POST',
			success (res) {
				// 数据清空
				setTimeout(function(){
					wx.hideLoading()
					that.setData({
						fromdata: [{
							'Name':'',
							'Phone':''
						}],
						show: !show
					})
        },300)
			},
			fail (res) {
				wx.showToast({
          title: '提交失败',
          icon: 'error'
        })
			},
			complete (res) {
				setTimeout(function(){
					wx.hideLoading()
        },300)
			}
		})
	},
	// 商城小程序
	shop() {
    wx.navigateToMiniProgram({
      appId: 'wx71dd1364b533ebed',//商场小程序appid
      path: 'pages/index/index',//跳转商场小程序页面
      extraData: {},
      envVersion: 'release',//跳转正式版
      success(res) { }
    })
	},
	// 客服 弹窗
  service() {
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  show() {
    this.setData({
      show: !this.data.show
    });
  },
  serviceimg() {
    wx.previewImage({
      current: this.data.serviceimg,
      urls: this.data.serviceimg
    })
  },
  enterpriseimg() {
    wx.previewImage({
      current: this.data.enterpriseimg,
      urls: this.data.enterpriseimg
    })
  },
  callme() {
    wx.makePhoneCall({
      phoneNumber: this.data.phone
    })
  },

	getName() {
		var familyNames = new Array(
			"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈",
			"褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
			"何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏",
			"陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章",
			"云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "鲁", "韦",
			"昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳",
			"酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺",
			"倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常",
			"乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余",
			"元", "卜", "顾", "孟", "平", "黄", "和", "穆", "萧", "尹"
		);
		var i = Math.floor(Math.random() * 100);
		var familyName = familyNames[i];
		return familyName + '**';
	},
	getMoble() {
		var prefixArray = new Array("130", "131", "132", "133", "135", "137", "138", "170", "187", "189");
		var i = parseInt(10 * Math.random());
		var prefix = prefixArray[i]+"****";
		for (var j = 0; j < 4; j++) {
			prefix = prefix  + Math.floor(Math.random() * 10);
		}
		return prefix
	},
	gettai(){
		return Math.floor(Math.random() * 10)
	},
	
	// 事件处理函数
	bindViewTap() {},
	onLoad() {
		// 数据拉取
		let FromMiniApp = "fromdata.FromMiniApp";
		const db = wx.cloud.database({
      env: 'cloud1-4gpheimm6b7530bb'
    });
		db.collection('datat').get({
      success: res => {
				console.log(res.data)
				for (var i=0;i<res.data.length;i++) {
					if (res.data[i]._id == 'bg') {
						this.setData({
							bgimg: res.data[i].src
						})
					}else if (res.data[i]._id == 'banner') {
						this.setData({
							bannerimg: res.data[i].src
						})
					}else if (res.data[i]._id == 'service') {
						this.setData({
							serviceimg: res.data[i].src
						})
					}else if (res.data[i]._id == 'enterprise') {
						this.setData({
							enterpriseimg: res.data[i].src
						})
					}else if (res.data[i]._id == 'phone') {
						this.setData({
							phone: res.data[i].phone
						})
					}else if (res.data[i]._id == 'app') {
						this.setData({
							[FromMiniApp]: res.data[i].app
						})
					}
				}
				
			}
		})

		// 轮播
		let data = []
		for (var j = 0; j < 15; j++) {
			data.push({
				name:this.getName(),
				phone:this.getMoble(),
				tai:this.gettai()
			})
    }
    console.log(data);
    this.setData({
      list :data
		})
		
		// 获取地区
		let addressData = "fromdata.Address";
		wx.getLocation({
      type: 'gcj02', //返回可以用于uni.openLocation的经纬度
      success:(res)=>{
        let that=this
          const latitude = res.latitude;
          const longitude = res.longitude;
        wx.request({
          header:{
            "Content-Type": "application/text"
          },
          //注意:这里的key值需要高德地图的 web服务生成的key  只有web服务才有逆地理编码
          url:'https://restapi.amap.com/v3/geocode/regeo?output=JSON&location='+res.longitude+','+res.latitude+'&key=280802ed0116fef931dbcf5e7e9278d7&radius=1000&extensions=all',
          success(re) {
            if(re.statusCode===200){
              that.citydata=re.data.regeocode.formatted_address
              console.log("获取中文街道地理位置成功",that.citydata)
              that.setData({
                [addressData]: that.citydata
              })
            }else{
              console.log("获取信息失败，请重试！")
            }
          }
        });
        }
    });
	},
})
